﻿package com.pandemicode.framework.smtp.collections {
	import com.pandemicode.framework.smtp.mailer.EmailAddress;
	
	/**
	* @author Mike Lawrence
	*/
	public class EmailAddressCollection extends Array {
		public function get formatted():String {
			var s:String = "";
			var i:int = 0;
			for each(var address:EmailAddress in this) {
				s += address.formatted;
				i++;
				if (i < this.length - 1)
					s += ",";
			}
			return s;
		}
		
		public function get formattedWithoutNames():String {
			var s:String = "";
			var i:int = 0;
			for each(var address:EmailAddress in this) {
				s += address.formattedWithoutName;
				i++;
				if (i < this.length - 1)
					s += ",";
			}
			return s;
		}
		
		public function EmailAddressCollection() {}
		
		public function add(emailAddress:EmailAddress):uint {
			return this.push(emailAddress);
		}
		
		public function get(index:int):EmailAddress {
			if (index > this.length - 1)
				return null;
			return this[index] as EmailAddress;
		}
		
		public function remove(emailAddress:EmailAddress):Boolean {
			for (var i:int = 0; i < this.length; i++) {
				var add:EmailAddress = this[i] as EmailAddress;
				if (add == emailAddress) {
					this.splice(i, 1);
					return true;
				}
			}
			return false;
		}
		
		public function removeAt(index:int):Boolean {
			if (index > this.length - 1)
				return false;
				
			this.splice(index, 1);
			return true;
		}
		
		public function toString():String {
			return this.formatted;
		}
	}
}
